package io.renren.modules.order.controller;

import io.renren.common.utils.R;
import io.renren.modules.dishes.entity.MerchantEntity;
import io.renren.modules.merchant.service.MerchantService;
import io.renren.modules.order.enity.MerchantEntityOrder;
import io.renren.modules.order.service.MerchantOrderService;
import io.renren.modules.order.service.OrderService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

import static io.renren.common.utils.ShiroUtils.getUserId;

@RestController
@RequestMapping("/MerchantOrder")
public class MerchantOrderController {
    @Resource
   private MerchantOrderService merchantOrderService;
    @Resource
    private MerchantService merchantService;

    @Resource
    private OrderService orderService;
    @RequestMapping("/list")
    public R list(@RequestParam Map<String,Object> params){
        Long userId = getUserId();
        MerchantEntity merchantByUserId = merchantService.getMerchantByUserId(userId);
        return R.ok().put("list",merchantOrderService.listByPage(merchantByUserId.getMerchantId(),params));
    }

    /**
     * 根据订单状态查询
     * @param params
     * @return
     */
   /* @RequestMapping("/MerchantOrderStatus")
    public R MerchantOrderStatusByPage(@RequestParam Map<String,Object> params){
        System.out.println(params.get("status"));
        Long userId = getUserId();
        String merchantId = merchantService.getMerchantByUserId(Long.valueOf(userId)).getMerchantId();
        params.put("merchantId",merchantId);
        String s = (String) params.get("status");
        int i = Integer.parseInt(s);
        return R.ok().put("list",merchantOrderService.MerchantOrderStatusByPage(params,i));
    }*/
    /**
     * 获取所有订单
     */
    @RequestMapping("/All")
    public R MerchantOrderList(){
        Long userId=getUserId();
        MerchantEntity merchantByUserId = merchantService.getMerchantByUserId(userId);
        return R.ok().put("list",merchantOrderService.queryListMerchantOrder(merchantByUserId.getMerchantId()));
    }

    /**
     * 根据orderCode进行发货
     * @param orderCode
     * @return
     */
    @RequestMapping("/send")
    public R send(@RequestParam("orderCode")String orderCode){
        orderService.updateOrderStatus(orderCode,2);
        return R.ok();
    }
    /**
     *
     */
    @RequestMapping("/getInfo/{orderCode}")
    public R getInfo(@PathVariable("orderCode") String orderCode){
        try {
            return R.ok().put("infoOrder",merchantOrderService.getDetails(orderCode));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
